package com.shanghaionstar.user.presenter;

import android.content.Context;

import com.onstar.cn.sqlite.entities.BroadcastHeaderEntity;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.user.iview.IBroadcastDetailView;
import com.shanghaionstar.user.service.BroadcastDetailService;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/8/9.
 */
public class BroadcastDetailPresenter  extends BasePresenter {

    private IBroadcastDetailView detailView;

    private BroadcastDetailService detailService;

    public BroadcastDetailPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        detailService = new BroadcastDetailService(context);
    }

    public void setBroadcastDetailView(IBroadcastDetailView detailView) {
        this.detailView = detailView;
    }

    public void loadMsgs(String msgId) {
        subscription = detailService.loadMsgDetail(msgId)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BroadcastHeaderEntity>() {
                    @Override
                    public void call(BroadcastHeaderEntity entity) {
                        if (detailView != null){
                            detailView.showDetail(entity);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (detailView != null && throwable != null) {
                            throwable.printStackTrace();
                            detailView.showErr(throwable);
                        }
                    }
                });
    }
    
}
